{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "# import torch.nn.functional as F\n",
    " \n",
    "# 准备数据集\n",
    "x_data = torch.Tensor([[1.0], [2.0], [3.0]])\n",
    "y_data = torch.Tensor([[0], [0], [1]])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义模型 \n",
    "class LogisticRegressionModel(torch.nn.Module):\n",
    "    def __init__(self):\n",
    "        super(LogisticRegressionModel, self).__init__()\n",
    "        self.linear = torch.nn.Linear(1,1)\n",
    " \n",
    "    def forward(self, x):\n",
    "        # y_pred = F.sigmoid(self.linear(x))\n",
    "        y_pred = torch.sigmoid(self.linear(x))\n",
    "        return y_pred\n",
    "model = LogisticRegressionModel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# construct loss and optimizer\n",
    "# 默认情况下，loss会基于element平均，如果size_average=False的话，loss会被累加。\n",
    "criterion = torch.nn.BCELoss(size_average = False) \n",
    "optimizer = torch.optim.SGD(model.parameters(), lr = 0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 2.014616012573242\n",
      "1 1.99175226688385\n",
      "2 1.9702131748199463\n",
      "3 1.9499410390853882\n",
      "4 1.930877447128296\n",
      "5 1.9129638671875\n",
      "6 1.8961424827575684\n",
      "7 1.8803563117980957\n",
      "8 1.8655489683151245\n",
      "9 1.8516658544540405\n",
      "10 1.8386532068252563\n",
      "11 1.8264597654342651\n",
      "12 1.8150355815887451\n",
      "13 1.804332971572876\n",
      "14 1.7943061590194702\n",
      "15 1.7849111557006836\n",
      "16 1.776106595993042\n",
      "17 1.7678529024124146\n",
      "18 1.7601124048233032\n",
      "19 1.7528504133224487\n",
      "20 1.7460330724716187\n",
      "21 1.7396290302276611\n",
      "22 1.7336093187332153\n",
      "23 1.7279460430145264\n",
      "24 1.7226133346557617\n",
      "25 1.7175872325897217\n",
      "26 1.7128448486328125\n",
      "27 1.7083656787872314\n",
      "28 1.7041298151016235\n",
      "29 1.7001192569732666\n",
      "30 1.6963167190551758\n",
      "31 1.692706823348999\n",
      "32 1.6892749071121216\n",
      "33 1.6860074996948242\n",
      "34 1.682891845703125\n",
      "35 1.6799166202545166\n",
      "36 1.6770710945129395\n",
      "37 1.6743452548980713\n",
      "38 1.6717298030853271\n",
      "39 1.6692163944244385\n",
      "40 1.666797161102295\n",
      "41 1.6644647121429443\n",
      "42 1.6622127294540405\n",
      "43 1.6600346565246582\n",
      "44 1.657925009727478\n",
      "45 1.6558785438537598\n",
      "46 1.653890609741211\n",
      "47 1.6519560813903809\n",
      "48 1.6500718593597412\n",
      "49 1.648233413696289\n",
      "50 1.646437168121338\n",
      "51 1.6446805000305176\n",
      "52 1.6429600715637207\n",
      "53 1.6412733793258667\n",
      "54 1.6396174430847168\n",
      "55 1.6379904747009277\n",
      "56 1.636390209197998\n",
      "57 1.6348145008087158\n",
      "58 1.633261799812317\n",
      "59 1.6317301988601685\n",
      "60 1.630218267440796\n",
      "61 1.6287248134613037\n",
      "62 1.6272485256195068\n",
      "63 1.625787615776062\n",
      "64 1.6243414878845215\n",
      "65 1.6229093074798584\n",
      "66 1.6214897632598877\n",
      "67 1.6200820207595825\n",
      "68 1.618685245513916\n",
      "69 1.6172995567321777\n",
      "70 1.6159230470657349\n",
      "71 1.614555835723877\n",
      "72 1.6131973266601562\n",
      "73 1.6118464469909668\n",
      "74 1.6105036735534668\n",
      "75 1.6091675758361816\n",
      "76 1.60783851146698\n",
      "77 1.606515645980835\n",
      "78 1.6051989793777466\n",
      "79 1.603887915611267\n",
      "80 1.6025822162628174\n",
      "81 1.6012816429138184\n",
      "82 1.5999860763549805\n",
      "83 1.5986948013305664\n",
      "84 1.5974082946777344\n",
      "85 1.596125841140747\n",
      "86 1.594847559928894\n",
      "87 1.5935735702514648\n",
      "88 1.5923030376434326\n",
      "89 1.5910359621047974\n",
      "90 1.5897724628448486\n",
      "91 1.5885125398635864\n",
      "92 1.5872557163238525\n",
      "93 1.5860018730163574\n",
      "94 1.5847513675689697\n",
      "95 1.5835039615631104\n",
      "96 1.5822592973709106\n",
      "97 1.5810174942016602\n",
      "98 1.5797784328460693\n",
      "99 1.5785419940948486\n",
      "100 1.5773084163665771\n",
      "101 1.5760771036148071\n",
      "102 1.5748486518859863\n",
      "103 1.5736225843429565\n",
      "104 1.5723989009857178\n",
      "105 1.5711777210235596\n",
      "106 1.5699589252471924\n",
      "107 1.5687425136566162\n",
      "108 1.567528247833252\n",
      "109 1.5663163661956787\n",
      "110 1.5651066303253174\n",
      "111 1.563899278640747\n",
      "112 1.5626940727233887\n",
      "113 1.5614910125732422\n",
      "114 1.5602900981903076\n",
      "115 1.559091567993164\n",
      "116 1.5578947067260742\n",
      "117 1.5567002296447754\n",
      "118 1.5555078983306885\n",
      "119 1.5543174743652344\n",
      "120 1.5531291961669922\n",
      "121 1.551943063735962\n",
      "122 1.5507588386535645\n",
      "123 1.549576759338379\n",
      "124 1.548396348953247\n",
      "125 1.5472180843353271\n",
      "126 1.5460420846939087\n",
      "127 1.544867753982544\n",
      "128 1.5436955690383911\n",
      "129 1.542525291442871\n",
      "130 1.5413570404052734\n",
      "131 1.5401906967163086\n",
      "132 1.5390260219573975\n",
      "133 1.5378637313842773\n",
      "134 1.5367028713226318\n",
      "135 1.5355442762374878\n",
      "136 1.5343875885009766\n",
      "137 1.5332324504852295\n",
      "138 1.5320796966552734\n",
      "139 1.5309284925460815\n",
      "140 1.5297791957855225\n",
      "141 1.5286320447921753\n",
      "142 1.5274865627288818\n",
      "143 1.5263431072235107\n",
      "144 1.5252010822296143\n",
      "145 1.5240613222122192\n",
      "146 1.5229233503341675\n",
      "147 1.521787166595459\n",
      "148 1.5206530094146729\n",
      "149 1.5195202827453613\n",
      "150 1.5183895826339722\n",
      "151 1.5172607898712158\n",
      "152 1.5161337852478027\n",
      "153 1.515008568763733\n",
      "154 1.513885259628296\n",
      "155 1.512763500213623\n",
      "156 1.5116437673568726\n",
      "157 1.5105258226394653\n",
      "158 1.509409785270691\n",
      "159 1.5082952976226807\n",
      "160 1.5071828365325928\n",
      "161 1.5060720443725586\n",
      "162 1.5049630403518677\n",
      "163 1.503855586051941\n",
      "164 1.5027501583099365\n",
      "165 1.5016465187072754\n",
      "166 1.500544548034668\n",
      "167 1.4994443655014038\n",
      "168 1.4983458518981934\n",
      "169 1.4972491264343262\n",
      "170 1.4961541891098022\n",
      "171 1.4950611591339111\n",
      "172 1.4939696788787842\n",
      "173 1.492879867553711\n",
      "174 1.4917919635772705\n",
      "175 1.4907057285308838\n",
      "176 1.4896211624145508\n",
      "177 1.4885382652282715\n",
      "178 1.4874573945999146\n",
      "179 1.4863779544830322\n",
      "180 1.4853001832962036\n",
      "181 1.4842242002487183\n",
      "182 1.4831500053405762\n",
      "183 1.4820774793624878\n",
      "184 1.4810066223144531\n",
      "185 1.4799375534057617\n",
      "186 1.478870153427124\n",
      "187 1.477804183959961\n",
      "188 1.4767401218414307\n",
      "189 1.475677728652954\n",
      "190 1.4746170043945312\n",
      "191 1.473557949066162\n",
      "192 1.4725004434585571\n",
      "193 1.4714446067810059\n",
      "194 1.470390796661377\n",
      "195 1.4693381786346436\n",
      "196 1.468287706375122\n",
      "197 1.467238426208496\n",
      "198 1.4661909341812134\n",
      "199 1.4651451110839844\n",
      "200 1.4641008377075195\n",
      "201 1.4630584716796875\n",
      "202 1.46201753616333\n",
      "203 1.4609782695770264\n",
      "204 1.4599406719207764\n",
      "205 1.458904504776001\n",
      "206 1.4578700065612793\n",
      "207 1.4568371772766113\n",
      "208 1.455806016921997\n",
      "209 1.4547765254974365\n",
      "210 1.4537484645843506\n",
      "211 1.4527218341827393\n",
      "212 1.4516971111297607\n",
      "213 1.450674295425415\n",
      "214 1.4496526718139648\n",
      "215 1.4486324787139893\n",
      "216 1.4476139545440674\n",
      "217 1.4465970993041992\n",
      "218 1.4455821514129639\n",
      "219 1.444568395614624\n",
      "220 1.4435561895370483\n",
      "221 1.4425458908081055\n",
      "222 1.4415366649627686\n",
      "223 1.440529227256775\n",
      "224 1.439523458480835\n",
      "225 1.4385192394256592\n",
      "226 1.437516450881958\n",
      "227 1.4365155696868896\n",
      "228 1.4355156421661377\n",
      "229 1.434517502784729\n",
      "230 1.433521032333374\n",
      "231 1.4325259923934937\n",
      "232 1.431532382965088\n",
      "233 1.4305404424667358\n",
      "234 1.429550051689148\n",
      "235 1.4285610914230347\n",
      "236 1.427573800086975\n",
      "237 1.4265879392623901\n",
      "238 1.4256036281585693\n",
      "239 1.4246208667755127\n",
      "240 1.4236395359039307\n",
      "241 1.4226596355438232\n",
      "242 1.4216811656951904\n",
      "243 1.4207043647766113\n",
      "244 1.4197291135787964\n",
      "245 1.418755292892456\n",
      "246 1.4177827835083008\n",
      "247 1.4168119430541992\n",
      "248 1.4158425331115723\n",
      "249 1.41487455368042\n",
      "250 1.4139081239700317\n",
      "251 1.4129431247711182\n",
      "252 1.4119797945022583\n",
      "253 1.411017894744873\n",
      "254 1.4100573062896729\n",
      "255 1.4090980291366577\n",
      "256 1.4081405401229858\n",
      "257 1.407184362411499\n",
      "258 1.4062297344207764\n",
      "259 1.4052762985229492\n",
      "260 1.4043245315551758\n",
      "261 1.403374195098877\n",
      "262 1.4024252891540527\n",
      "263 1.401477575302124\n",
      "264 1.400531530380249\n",
      "265 1.3995871543884277\n",
      "266 1.3986437320709229\n",
      "267 1.3977019786834717\n",
      "268 1.3967616558074951\n",
      "269 1.3958226442337036\n",
      "270 1.3948851823806763\n",
      "271 1.393949270248413\n",
      "272 1.3930141925811768\n",
      "273 1.3920810222625732\n",
      "274 1.3911492824554443\n",
      "275 1.390218734741211\n",
      "276 1.3892898559570312\n",
      "277 1.388361930847168\n",
      "278 1.387435793876648\n",
      "279 1.3865108489990234\n",
      "280 1.385587453842163\n",
      "281 1.3846651315689087\n",
      "282 1.3837443590164185\n",
      "283 1.3828251361846924\n",
      "284 1.3819069862365723\n",
      "285 1.3809902667999268\n",
      "286 1.3800753355026245\n",
      "287 1.3791612386703491\n",
      "288 1.378248929977417\n",
      "289 1.3773374557495117\n",
      "290 1.3764278888702393\n",
      "291 1.3755195140838623\n",
      "292 1.3746124505996704\n",
      "293 1.3737066984176636\n",
      "294 1.3728021383285522\n",
      "295 1.371899127960205\n",
      "296 1.3709975481033325\n",
      "297 1.3700973987579346\n",
      "298 1.3691980838775635\n",
      "299 1.368300437927246\n",
      "300 1.3674042224884033\n",
      "301 1.366508960723877\n",
      "302 1.3656153678894043\n",
      "303 1.3647232055664062\n",
      "304 1.3638319969177246\n",
      "305 1.3629422187805176\n",
      "306 1.3620538711547852\n",
      "307 1.3611668348312378\n",
      "308 1.360281229019165\n",
      "309 1.3593964576721191\n",
      "310 1.358513355255127\n",
      "311 1.3576314449310303\n",
      "312 1.3567508459091187\n",
      "313 1.3558716773986816\n",
      "314 1.3549935817718506\n",
      "315 1.3541169166564941\n",
      "316 1.3532414436340332\n",
      "317 1.3523674011230469\n",
      "318 1.351494550704956\n",
      "319 1.3506232500076294\n",
      "320 1.3497527837753296\n",
      "321 1.348883867263794\n",
      "322 1.3480161428451538\n",
      "323 1.3471494913101196\n",
      "324 1.3462843894958496\n",
      "325 1.3454205989837646\n",
      "326 1.344557762145996\n",
      "327 1.3436965942382812\n",
      "328 1.3428363800048828\n",
      "329 1.3419773578643799\n",
      "330 1.3411197662353516\n",
      "331 1.3402633666992188\n",
      "332 1.3394081592559814\n",
      "333 1.3385546207427979\n",
      "334 1.3377020359039307\n",
      "335 1.3368502855300903\n",
      "336 1.3360002040863037\n",
      "337 1.335151195526123\n",
      "338 1.3343037366867065\n",
      "339 1.3334572315216064\n",
      "340 1.3326117992401123\n",
      "341 1.3317677974700928\n",
      "342 1.3309249877929688\n",
      "343 1.3300833702087402\n",
      "344 1.3292431831359863\n",
      "345 1.3284039497375488\n",
      "346 1.3275660276412964\n",
      "347 1.32672917842865\n",
      "348 1.3258936405181885\n",
      "349 1.3250595331192017\n",
      "350 1.3242262601852417\n",
      "351 1.3233944177627563\n",
      "352 1.3225637674331665\n",
      "353 1.3217341899871826\n",
      "354 1.3209059238433838\n",
      "355 1.32007896900177\n",
      "356 1.3192529678344727\n",
      "357 1.3184281587600708\n",
      "358 1.3176045417785645\n",
      "359 1.3167821168899536\n",
      "360 1.3159608840942383\n",
      "361 1.315140962600708\n",
      "362 1.3143222332000732\n",
      "363 1.3135042190551758\n",
      "364 1.312687873840332\n",
      "365 1.3118724822998047\n",
      "366 1.3110582828521729\n",
      "367 1.3102452754974365\n",
      "368 1.3094334602355957\n",
      "369 1.3086225986480713\n",
      "370 1.307813048362732\n",
      "371 1.307004690170288\n",
      "372 1.3061974048614502\n",
      "373 1.3053913116455078\n",
      "374 1.3045865297317505\n",
      "375 1.303782343864441\n",
      "376 1.302979826927185\n",
      "377 1.3021783828735352\n",
      "378 1.3013780117034912\n",
      "379 1.3005788326263428\n",
      "380 1.2997806072235107\n",
      "381 1.2989835739135742\n",
      "382 1.2981876134872437\n",
      "383 1.2973929643630981\n",
      "384 1.2965993881225586\n",
      "385 1.295806884765625\n",
      "386 1.295015573501587\n",
      "387 1.2942250967025757\n",
      "388 1.2934359312057495\n",
      "389 1.2926478385925293\n",
      "390 1.2918610572814941\n",
      "391 1.2910752296447754\n",
      "392 1.290290355682373\n",
      "393 1.2895067930221558\n",
      "394 1.2887243032455444\n",
      "395 1.287942886352539\n",
      "396 1.28716242313385\n",
      "397 1.2863831520080566\n",
      "398 1.2856050729751587\n",
      "399 1.2848280668258667\n",
      "400 1.2840521335601807\n",
      "401 1.2832772731781006\n",
      "402 1.2825034856796265\n",
      "403 1.2817307710647583\n",
      "404 1.2809590101242065\n",
      "405 1.2801886796951294\n",
      "406 1.2794190645217896\n",
      "407 1.2786506414413452\n",
      "408 1.2778832912445068\n",
      "409 1.2771170139312744\n",
      "410 1.276351809501648\n",
      "411 1.2755876779556274\n",
      "412 1.2748243808746338\n",
      "413 1.2740626335144043\n",
      "414 1.273301601409912\n",
      "415 1.2725416421890259\n",
      "416 1.271782636642456\n",
      "417 1.2710249423980713\n",
      "418 1.270268201828003\n",
      "419 1.26951265335083\n",
      "420 1.268757939338684\n",
      "421 1.268004298210144\n",
      "422 1.26725172996521\n",
      "423 1.2664999961853027\n",
      "424 1.265749454498291\n",
      "425 1.2650001049041748\n",
      "426 1.2642518281936646\n",
      "427 1.2635045051574707\n",
      "428 1.2627580165863037\n",
      "429 1.2620126008987427\n",
      "430 1.2612683773040771\n",
      "431 1.260525107383728\n",
      "432 1.2597827911376953\n",
      "433 1.259041428565979\n",
      "434 1.2583012580871582\n",
      "435 1.2575619220733643\n",
      "436 1.2568237781524658\n",
      "437 1.2560865879058838\n",
      "438 1.2553504705429077\n",
      "439 1.254615306854248\n",
      "440 1.2538810968399048\n",
      "441 1.2531479597091675\n",
      "442 1.2524157762527466\n",
      "443 1.251684546470642\n",
      "444 1.2509543895721436\n",
      "445 1.250225305557251\n",
      "446 1.2494969367980957\n",
      "447 1.248769760131836\n",
      "448 1.2480435371398926\n",
      "449 1.2473182678222656\n",
      "450 1.2465941905975342\n",
      "451 1.24587082862854\n",
      "452 1.2451485395431519\n",
      "453 1.24442720413208\n",
      "454 1.2437069416046143\n",
      "455 1.2429876327514648\n",
      "456 1.2422692775726318\n",
      "457 1.2415518760681152\n",
      "458 1.240835428237915\n",
      "459 1.2401199340820312\n",
      "460 1.2394055128097534\n",
      "461 1.238692045211792\n",
      "462 1.237979531288147\n",
      "463 1.2372678518295288\n",
      "464 1.2365570068359375\n",
      "465 1.2358474731445312\n",
      "466 1.2351388931274414\n",
      "467 1.2344310283660889\n",
      "468 1.2337239980697632\n",
      "469 1.233018159866333\n",
      "470 1.2323133945465088\n",
      "471 1.2316093444824219\n",
      "472 1.2309062480926514\n",
      "473 1.2302042245864868\n",
      "474 1.2295030355453491\n",
      "475 1.2288028001785278\n",
      "476 1.2281033992767334\n",
      "477 1.227405071258545\n",
      "478 1.2267076969146729\n",
      "479 1.2260111570358276\n",
      "480 1.225315809249878\n",
      "481 1.224621057510376\n",
      "482 1.22392737865448\n",
      "483 1.2232346534729004\n",
      "484 1.2225427627563477\n",
      "485 1.2218518257141113\n",
      "486 1.2211618423461914\n",
      "487 1.220472812652588\n",
      "488 1.2197843790054321\n",
      "489 1.219097375869751\n",
      "490 1.2184109687805176\n",
      "491 1.2177255153656006\n",
      "492 1.217040777206421\n",
      "493 1.2163572311401367\n",
      "494 1.2156745195388794\n",
      "495 1.214992642402649\n",
      "496 1.2143118381500244\n",
      "497 1.2136316299438477\n",
      "498 1.2129526138305664\n",
      "499 1.212274432182312\n",
      "500 1.2115968465805054\n",
      "501 1.2109206914901733\n",
      "502 1.21024489402771\n",
      "503 1.2095704078674316\n",
      "504 1.2088963985443115\n",
      "505 1.208223581314087\n",
      "506 1.2075515985488892\n",
      "507 1.2068805694580078\n",
      "508 1.2062101364135742\n",
      "509 1.2055408954620361\n",
      "510 1.2048723697662354\n",
      "511 1.204204797744751\n",
      "512 1.203537940979004\n",
      "513 1.2028721570968628\n",
      "514 1.2022072076797485\n",
      "515 1.2015429735183716\n",
      "516 1.2008798122406006\n",
      "517 1.2002172470092773\n",
      "518 1.1995558738708496\n",
      "519 1.1988950967788696\n",
      "520 1.198235273361206\n",
      "521 1.1975762844085693\n",
      "522 1.1969181299209595\n",
      "523 1.1962611675262451\n",
      "524 1.195604681968689\n",
      "525 1.1949491500854492\n",
      "526 1.1942944526672363\n",
      "527 1.1936404705047607\n",
      "528 1.1929875612258911\n",
      "529 1.192335605621338\n",
      "530 1.1916841268539429\n",
      "531 1.1910336017608643\n",
      "532 1.1903841495513916\n",
      "533 1.1897354125976562\n",
      "534 1.1890873908996582\n",
      "535 1.1884400844573975\n",
      "536 1.1877938508987427\n",
      "537 1.1871486902236938\n",
      "538 1.1865040063858032\n",
      "539 1.185860276222229\n",
      "540 1.185217261314392\n",
      "541 1.1845752000808716\n",
      "542 1.1839338541030884\n",
      "543 1.1832934617996216\n",
      "544 1.182653784751892\n",
      "545 1.1820148229599\n",
      "546 1.1813768148422241\n",
      "547 1.1807397603988647\n",
      "548 1.1801033020019531\n",
      "549 1.1794679164886475\n",
      "550 1.1788330078125\n",
      "551 1.1781991720199585\n",
      "552 1.1775659322738647\n",
      "553 1.1769338846206665\n",
      "554 1.176302194595337\n",
      "555 1.1756714582443237\n",
      "556 1.1750415563583374\n",
      "557 1.174412488937378\n",
      "558 1.1737840175628662\n",
      "559 1.17315673828125\n",
      "560 1.172529935836792\n",
      "561 1.1719039678573608\n",
      "562 1.171278953552246\n",
      "563 1.170654535293579\n",
      "564 1.170031189918518\n",
      "565 1.1694083213806152\n",
      "566 1.1687865257263184\n",
      "567 1.1681653261184692\n",
      "568 1.1675447225570679\n",
      "569 1.1669254302978516\n",
      "570 1.166306495666504\n",
      "571 1.165688395500183\n",
      "572 1.1650711297988892\n",
      "573 1.1644548177719116\n",
      "574 1.1638391017913818\n",
      "575 1.1632241010665894\n",
      "576 1.1626100540161133\n",
      "577 1.1619964838027954\n",
      "578 1.1613839864730835\n",
      "579 1.1607723236083984\n",
      "580 1.160161018371582\n",
      "581 1.1595509052276611\n",
      "582 1.1589412689208984\n",
      "583 1.1583327054977417\n",
      "584 1.1577246189117432\n",
      "585 1.1571173667907715\n",
      "586 1.156510829925537\n",
      "587 1.1559052467346191\n",
      "588 1.1553001403808594\n",
      "589 1.154695987701416\n",
      "590 1.1540926694869995\n",
      "591 1.1534899473190308\n",
      "592 1.1528878211975098\n",
      "593 1.1522866487503052\n",
      "594 1.1516863107681274\n",
      "595 1.1510865688323975\n",
      "596 1.1504876613616943\n",
      "597 1.149889349937439\n",
      "598 1.1492918729782104\n",
      "599 1.1486952304840088\n",
      "600 1.1480993032455444\n",
      "601 1.1475040912628174\n",
      "602 1.1469093561172485\n",
      "603 1.1463156938552856\n",
      "604 1.1457226276397705\n",
      "605 1.1451303958892822\n",
      "606 1.1445386409759521\n",
      "607 1.143947958946228\n",
      "608 1.1433576345443726\n",
      "609 1.1427685022354126\n",
      "610 1.1421797275543213\n",
      "611 1.1415917873382568\n",
      "612 1.1410045623779297\n",
      "613 1.140418291091919\n",
      "614 1.1398323774337769\n",
      "615 1.1392474174499512\n",
      "616 1.1386630535125732\n",
      "617 1.1380795240402222\n",
      "618 1.1374965906143188\n",
      "619 1.1369144916534424\n",
      "620 1.1363329887390137\n",
      "621 1.1357520818710327\n",
      "622 1.1351721286773682\n",
      "623 1.1345930099487305\n",
      "624 1.134014368057251\n",
      "625 1.1334364414215088\n",
      "626 1.132859230041504\n",
      "627 1.1322824954986572\n",
      "628 1.131706953048706\n",
      "629 1.131131649017334\n",
      "630 1.1305572986602783\n",
      "631 1.1299837827682495\n",
      "632 1.1294105052947998\n",
      "633 1.128838300704956\n",
      "634 1.1282665729522705\n",
      "635 1.1276957988739014\n",
      "636 1.1271257400512695\n",
      "637 1.126556158065796\n",
      "638 1.1259872913360596\n",
      "639 1.1254191398620605\n",
      "640 1.1248518228530884\n",
      "641 1.1242849826812744\n",
      "642 1.1237189769744873\n",
      "643 1.123153567314148\n",
      "644 1.122588872909546\n",
      "645 1.1220247745513916\n",
      "646 1.1214613914489746\n",
      "647 1.120898723602295\n",
      "648 1.1203367710113525\n",
      "649 1.1197755336761475\n",
      "650 1.1192148923873901\n",
      "651 1.118654727935791\n",
      "652 1.1180956363677979\n",
      "653 1.1175367832183838\n",
      "654 1.1169788837432861\n",
      "655 1.1164216995239258\n",
      "656 1.1158649921417236\n",
      "657 1.115309238433838\n",
      "658 1.1147537231445312\n",
      "659 1.114199161529541\n",
      "660 1.113645315170288\n",
      "661 1.113092064857483\n",
      "662 1.1125391721725464\n",
      "663 1.1119874715805054\n",
      "664 1.111436128616333\n",
      "665 1.1108856201171875\n",
      "666 1.1103355884552002\n",
      "667 1.109786033630371\n",
      "668 1.109237551689148\n",
      "669 1.108689546585083\n",
      "670 1.1081420183181763\n",
      "671 1.107595443725586\n",
      "672 1.1070493459701538\n",
      "673 1.106503963470459\n",
      "674 1.1059590578079224\n",
      "675 1.1054149866104126\n",
      "676 1.104871392250061\n",
      "677 1.1043287515640259\n",
      "678 1.1037863492965698\n",
      "679 1.1032449007034302\n",
      "680 1.1027038097381592\n",
      "681 1.102163553237915\n",
      "682 1.1016238927841187\n",
      "683 1.1010850667953491\n",
      "684 1.1005468368530273\n",
      "685 1.1000088453292847\n",
      "686 1.0994716882705688\n",
      "687 1.0989353656768799\n",
      "688 1.0983996391296387\n",
      "689 1.0978643894195557\n",
      "690 1.0973296165466309\n",
      "691 1.096795678138733\n",
      "692 1.0962624549865723\n",
      "693 1.0957298278808594\n",
      "694 1.0951976776123047\n",
      "695 1.0946662425994873\n",
      "696 1.0941352844238281\n",
      "697 1.0936052799224854\n",
      "698 1.0930757522583008\n",
      "699 1.092546820640564\n",
      "700 1.0920182466506958\n",
      "701 1.091490626335144\n",
      "702 1.090963363647461\n",
      "703 1.0904369354248047\n",
      "704 1.0899109840393066\n",
      "705 1.0893858671188354\n",
      "706 1.0888609886169434\n",
      "707 1.0883370637893677\n",
      "708 1.0878136157989502\n",
      "709 1.0872907638549805\n",
      "710 1.0867685079574585\n",
      "711 1.0862469673156738\n",
      "712 1.085726022720337\n",
      "713 1.0852055549621582\n",
      "714 1.0846856832504272\n",
      "715 1.084166407585144\n",
      "716 1.0836477279663086\n",
      "717 1.0831297636032104\n",
      "718 1.0826122760772705\n",
      "719 1.0820955038070679\n",
      "720 1.0815792083740234\n",
      "721 1.0810636281967163\n",
      "722 1.080548644065857\n",
      "723 1.0800342559814453\n",
      "724 1.0795204639434814\n",
      "725 1.0790070295333862\n",
      "726 1.0784944295883179\n",
      "727 1.0779823064804077\n",
      "728 1.0774708986282349\n",
      "729 1.0769597291946411\n",
      "730 1.0764495134353638\n",
      "731 1.0759397745132446\n",
      "732 1.0754308700561523\n",
      "733 1.0749223232269287\n",
      "734 1.0744141340255737\n",
      "735 1.0739067792892456\n",
      "736 1.0734000205993652\n",
      "737 1.0728938579559326\n",
      "738 1.0723880529403687\n",
      "739 1.0718830823898315\n",
      "740 1.071378469467163\n",
      "741 1.070874571800232\n",
      "742 1.070371150970459\n",
      "743 1.069868564605713\n",
      "744 1.069366216659546\n",
      "745 1.0688644647598267\n",
      "746 1.0683635473251343\n",
      "747 1.0678629875183105\n",
      "748 1.0673631429672241\n",
      "749 1.0668638944625854\n",
      "750 1.0663650035858154\n",
      "751 1.0658667087554932\n",
      "752 1.0653690099716187\n",
      "753 1.064871907234192\n",
      "754 1.064375400543213\n",
      "755 1.0638794898986816\n",
      "756 1.0633840560913086\n",
      "757 1.0628890991210938\n",
      "758 1.0623949766159058\n",
      "759 1.0619010925292969\n",
      "760 1.0614079236984253\n",
      "761 1.060915231704712\n",
      "762 1.0604233741760254\n",
      "763 1.059931755065918\n",
      "764 1.0594407320022583\n",
      "765 1.058950424194336\n",
      "766 1.0584604740142822\n",
      "767 1.0579710006713867\n",
      "768 1.057482361793518\n",
      "769 1.0569941997528076\n",
      "770 1.0565063953399658\n",
      "771 1.0560194253921509\n",
      "772 1.0555328130722046\n",
      "773 1.055046796798706\n",
      "774 1.0545613765716553\n",
      "775 1.0540763139724731\n",
      "776 1.0535919666290283\n",
      "777 1.0531079769134521\n",
      "778 1.0526247024536133\n",
      "779 1.0521419048309326\n",
      "780 1.0516595840454102\n",
      "781 1.0511778593063354\n",
      "782 1.0506967306137085\n",
      "783 1.0502160787582397\n",
      "784 1.0497360229492188\n",
      "785 1.0492563247680664\n",
      "786 1.0487775802612305\n",
      "787 1.0482988357543945\n",
      "788 1.047820806503296\n",
      "789 1.0473432540893555\n",
      "790 1.0468664169311523\n",
      "791 1.0463900566101074\n",
      "792 1.0459141731262207\n",
      "793 1.0454387664794922\n",
      "794 1.044964075088501\n",
      "795 1.0444897413253784\n",
      "796 1.0440160036087036\n",
      "797 1.043542742729187\n",
      "798 1.0430699586868286\n",
      "799 1.0425976514816284\n",
      "800 1.0421260595321655\n",
      "801 1.0416549444198608\n",
      "802 1.0411841869354248\n",
      "803 1.0407140254974365\n",
      "804 1.040244460105896\n",
      "805 1.0397752523422241\n",
      "806 1.039306640625\n",
      "807 1.0388388633728027\n",
      "808 1.0383710861206055\n",
      "809 1.0379040241241455\n",
      "810 1.0374375581741333\n",
      "811 1.0369715690612793\n",
      "812 1.036505937576294\n",
      "813 1.036041021347046\n",
      "814 1.035576581954956\n",
      "815 1.0351123809814453\n",
      "816 1.0346490144729614\n",
      "817 1.0341860055923462\n",
      "818 1.0337235927581787\n",
      "819 1.0332616567611694\n",
      "820 1.0327999591827393\n",
      "821 1.032339096069336\n",
      "822 1.0318787097930908\n",
      "823 1.0314186811447144\n",
      "824 1.0309592485427856\n",
      "825 1.0305002927780151\n",
      "826 1.0300415754318237\n",
      "827 1.0295836925506592\n",
      "828 1.0291262865066528\n",
      "829 1.0286693572998047\n",
      "830 1.0282127857208252\n",
      "831 1.0277568101882935\n",
      "832 1.0273014307022095\n",
      "833 1.0268462896347046\n",
      "834 1.0263917446136475\n",
      "835 1.0259376764297485\n",
      "836 1.0254842042922974\n",
      "837 1.0250310897827148\n",
      "838 1.0245786905288696\n",
      "839 1.024126648902893\n",
      "840 1.0236748456954956\n",
      "841 1.0232237577438354\n",
      "842 1.0227731466293335\n",
      "843 1.0223230123519897\n",
      "844 1.0218733549118042\n",
      "845 1.0214242935180664\n",
      "846 1.0209755897521973\n",
      "847 1.0205272436141968\n",
      "848 1.020079493522644\n",
      "849 1.0196322202682495\n",
      "850 1.0191854238510132\n",
      "851 1.018739104270935\n",
      "852 1.0182933807373047\n",
      "853 1.017848014831543\n",
      "854 1.0174031257629395\n",
      "855 1.0169587135314941\n",
      "856 1.0165146589279175\n",
      "857 1.0160714387893677\n",
      "858 1.0156283378601074\n",
      "859 1.0151857137680054\n",
      "860 1.0147438049316406\n",
      "861 1.0143022537231445\n",
      "862 1.013861060142517\n",
      "863 1.0134203433990479\n",
      "864 1.012980341911316\n",
      "865 1.012540578842163\n",
      "866 1.012101411819458\n",
      "867 1.011662483215332\n",
      "868 1.0112242698669434\n",
      "869 1.0107864141464233\n",
      "870 1.0103490352630615\n",
      "871 1.009912133216858\n",
      "872 1.0094757080078125\n",
      "873 1.0090397596359253\n",
      "874 1.0086040496826172\n",
      "875 1.008169174194336\n",
      "876 1.0077342987060547\n",
      "877 1.0073001384735107\n",
      "878 1.0068665742874146\n",
      "879 1.0064332485198975\n",
      "880 1.0060005187988281\n",
      "881 1.005568027496338\n",
      "882 1.0051363706588745\n",
      "883 1.0047049522399902\n",
      "884 1.004273772239685\n",
      "885 1.0038433074951172\n",
      "886 1.003413200378418\n",
      "887 1.002983570098877\n",
      "888 1.0025544166564941\n",
      "889 1.00212562084198\n",
      "890 1.001697301864624\n",
      "891 1.0012694597244263\n",
      "892 1.0008420944213867\n",
      "893 1.0004150867462158\n",
      "894 0.9999886751174927\n",
      "895 0.999562680721283\n",
      "896 0.9991370439529419\n",
      "897 0.9987117648124695\n",
      "898 0.9982872009277344\n",
      "899 0.997862696647644\n",
      "900 0.997438907623291\n",
      "901 0.9970153570175171\n",
      "902 0.9965925216674805\n",
      "903 0.9961700439453125\n",
      "904 0.9957478046417236\n",
      "905 0.9953261613845825\n",
      "906 0.9949050545692444\n",
      "907 0.9944843053817749\n",
      "908 0.9940638542175293\n",
      "909 0.9936438798904419\n",
      "910 0.993224561214447\n",
      "911 0.9928053617477417\n",
      "912 0.9923868179321289\n",
      "913 0.9919686317443848\n",
      "914 0.9915508031845093\n",
      "915 0.991133451461792\n",
      "916 0.9907165765762329\n",
      "917 0.9903000593185425\n",
      "918 0.9898840188980103\n",
      "919 0.9894682168960571\n",
      "920 0.9890532493591309\n",
      "921 0.9886384010314941\n",
      "922 0.9882241487503052\n",
      "923 0.9878101348876953\n",
      "924 0.9873965978622437\n",
      "925 0.9869836568832397\n",
      "926 0.9865708947181702\n",
      "927 0.9861586093902588\n",
      "928 0.9857468008995056\n",
      "929 0.9853354096412659\n",
      "930 0.9849244356155396\n",
      "931 0.9845139980316162\n",
      "932 0.9841036796569824\n",
      "933 0.9836941957473755\n",
      "934 0.9832848310470581\n",
      "935 0.9828759431838989\n",
      "936 0.9824676513671875\n",
      "937 0.9820593595504761\n",
      "938 0.981651782989502\n",
      "939 0.981244683265686\n",
      "940 0.9808378219604492\n",
      "941 0.9804314374923706\n",
      "942 0.9800254106521606\n",
      "943 0.9796199202537537\n",
      "944 0.9792147278785706\n",
      "945 0.9788100719451904\n",
      "946 0.9784054756164551\n",
      "947 0.9780018329620361\n",
      "948 0.9775981903076172\n",
      "949 0.9771950244903564\n",
      "950 0.9767926335334778\n",
      "951 0.9763901233673096\n",
      "952 0.9759882688522339\n",
      "953 0.9755867719650269\n",
      "954 0.9751855731010437\n",
      "955 0.9747850894927979\n",
      "956 0.9743847250938416\n",
      "957 0.9739848375320435\n",
      "958 0.973585307598114\n",
      "959 0.9731861352920532\n",
      "960 0.9727876782417297\n",
      "961 0.9723892211914062\n",
      "962 0.9719915390014648\n",
      "963 0.9715937376022339\n",
      "964 0.9711968898773193\n",
      "965 0.9708002805709839\n",
      "966 0.9704039096832275\n",
      "967 0.9700080156326294\n",
      "968 0.9696125388145447\n",
      "969 0.9692174792289734\n",
      "970 0.9688225984573364\n",
      "971 0.9684284925460815\n",
      "972 0.9680343866348267\n",
      "973 0.9676409959793091\n",
      "974 0.967247724533081\n",
      "975 0.9668550491333008\n",
      "976 0.9664624929428101\n",
      "977 0.9660707712173462\n",
      "978 0.9656792879104614\n",
      "979 0.9652880430221558\n",
      "980 0.9648971557617188\n",
      "981 0.9645068049430847\n",
      "982 0.9641168117523193\n",
      "983 0.9637269973754883\n",
      "984 0.9633378386497498\n",
      "985 0.9629490375518799\n",
      "986 0.9625605344772339\n",
      "987 0.9621724486351013\n",
      "988 0.9617847204208374\n",
      "989 0.9613973498344421\n",
      "990 0.9610104560852051\n",
      "991 0.9606238603591919\n",
      "992 0.9602376222610474\n",
      "993 0.9598517417907715\n",
      "994 0.9594664573669434\n",
      "995 0.9590814113616943\n",
      "996 0.9586966633796692\n",
      "997 0.9583123326301575\n",
      "998 0.9579284191131592\n",
      "999 0.9575449824333191\n",
      "w =  1.3649331331253052\n",
      "b =  -3.2922677993774414\n",
      "y_pred =  tensor([[0.8973]])\n"
     ]
    }
   ],
   "source": [
    "# training cycle forward, backward, update\n",
    "for epoch in range(1000):\n",
    "    y_pred = model(x_data)\n",
    "    loss = criterion(y_pred, y_data)\n",
    "    print(epoch, loss.item())\n",
    " \n",
    "    optimizer.zero_grad()\n",
    "    loss.backward()\n",
    "    optimizer.step()\n",
    " \n",
    "print('w = ', model.linear.weight.item())\n",
    "print('b = ', model.linear.bias.item())\n",
    " \n",
    "x_test = torch.Tensor([[4.0]])\n",
    "y_test = model(x_test)\n",
    "print('y_pred = ', y_test.data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
